package com.house.rent.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.base.lib.mvp.present.BasePresenter;
import com.house.rent.App;
import com.house.rent.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author neo.duan
 * @date 2019-09-21 22:38
 * @desc 修改密码
 */
public class ModifyPwdActivity extends BaseActivity {

    @BindView(R.id.edit_pwd)
    EditText mEdtPwd;
    @BindView(R.id.edit_confirm_pwd)
    EditText mEdtNewPwd;
    @BindView(R.id.edit_confirm_new_pwd)
    EditText mEdtRepeatPwd;

    public static void start(Context context) {
        Intent starter = new Intent(context, ModifyPwdActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void initTop() {
        setTitle("修改密码");
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_modify_pwd;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initListeners() {

    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.btn_modify_pwd})
    public void onViewClick(View view) {
        String currentPwd = getCurrentPwd();
        String newPwd = getNewPwd();
        String repeatPwd = getRepeatPwd();

        if (TextUtils.isEmpty(currentPwd)) {
            showMessage("请输入原密码");
            return;
        }
        if (TextUtils.isEmpty(newPwd)) {
            showMessage("请输入新密码");
            return;
        }

        if (newPwd.length() < 6 || newPwd.length() > 15) {
            showMessage("请输入6~15位新密码");
            return;
        }

        if (TextUtils.isEmpty(repeatPwd)) {
            showMessage("请再次输入新密码");
            return;
        }

        if (!newPwd.equals(repeatPwd)) {
            showMessage("两次输入新密码不一致");
            return;
        }

        showLoading("正在操作...");
        addParams("userId", App.getInstance().getUserEntity().getId());
        addParams("oldPassword", currentPwd);
        addParams("newPassword", newPwd);
        sendRequest("user/updatePassword", null);
    }

    @Override
    protected void onRequestSuccess(String reqId, Object obj) {
        super.onRequestSuccess(reqId, obj);
        showMessage("密码修改成功");
        finish();
    }

    private String getCurrentPwd() {
        return mEdtPwd.getText().toString().trim();
    }

    private String getNewPwd() {
        return mEdtNewPwd.getText().toString().trim();
    }

    private String getRepeatPwd() {
        return mEdtRepeatPwd.getText().toString().trim();
    }
}
